package com.rookie.im.learn.io.mq.producer.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rookie.im.learn.io.mq.producer.service.RabbitMQService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/rookie/rabbitmq")
public class RabbitController {
    @Resource
    private RabbitMQService rabbitMQService;
    @RequestMapping("direct")
    public String direct(String msg) {
        return  rabbitMQService.direct(msg);
    }
    @RequestMapping("fanout")
    public String fanout(String msg) {
        return  rabbitMQService.fanout(msg);
    }
    @RequestMapping("topic")
    public String topic(String msg, String key) {
        return  rabbitMQService.topic(msg, key);
    }
    @PostMapping("headers")
    public String headers(String msg, String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = mapper.readValue(json, Map.class);
        return  rabbitMQService.headers(msg, map);
    }
}
